#!/usr/bin/python
# -*- coding:utf-8 -*-  

import os
import smtplib
import traceback
from urlparse import urlparse
from multiprocessing import Process
from email import encoders
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import formatdate


#Email邮件类
class Email():
    __smtpServer="email.baidu.com"#SMTP服务器
   
    #发送带附件的邮件
    def sendAttEmail(self, server, fro, to, subject, text, files=[]):
        assert type(server) == dict
        assert type(to) == list
        assert type(files) == list
       
        COMMASPACE = ','
       
        msg = MIMEMultipart()
        msg['From'] = fro
        msg['Subject'] = subject
        msg['To'] = COMMASPACE.join(to)
        msg['Date'] = formatdate(localtime=True)
        msg.set_charset('utf-8')
        msg.attach(MIMEText(text))
       
        for file in files:
            part = MIMEBase('application', 'octet-stream')
            part.set_payload(open(file, 'rb').read())
            encoders.encode_base64(part)
            part.add_header('Content-Disposition', 'attachment; filename=" % s"' % os.path.basename(file))
            msg.attach(part)

        smtp = smtplib.SMTP(self.__smtpServer, 25)
        smtp.set_debuglevel(1)
        smtp.ehlo()
        smtp.starttls()
        smtp.ehlo()
        smtp.login(server.get('user'), server.get('passwd'))
        smtp.sendmail(fro, to, msg.as_string())
        smtp.close()        

    #发送HTML邮件
    def sendHtmlEmail(self, server, fro, to, cc,subject, html, files=[]):
        assert type(server) == dict
        assert type(to) == list
        assert type(cc) == list
        assert type(files) == list
       
        COMMASPACE = ','
       
        msg = MIMEMultipart('related')
        msg['From'] = fro
        msg['Subject'] = subject
        msg['To'] = COMMASPACE.join(to)
        msg['Cc'] = COMMASPACE.join(cc)
        msg['Date'] = formatdate(localtime=True)
        msg.set_charset('utf-8')
       
        msg.preamble = 'This is a multi-part message in MIME format.'
        msgAlternative = MIMEMultipart('alternative')    
        msg.attach(msgAlternative)  
       
        msgHtml = MIMEText(html, 'html', 'utf-8')    
        msgAlternative.attach(msgHtml)  
       
        i = 0
        for file in files:
            if file[-4:]=='.png':
                fp = open(file, 'rb')    
                msgImage = MIMEImage(fp.read())    
                fp.close()    
                msgImage.add_header('Content-ID', '<image%d>'%(i))    
                msg.attach(msgImage)          
                i += 1  
       
        self.__send(server, fro, to, msg)
   
    #发送HTML且带附件的邮件
    def sendHtmlAndAttachment(self, server, fro, to, cc,subject, html, files=[]):
        assert type(server) == dict
        assert type(to) == list
        assert type(cc) == list
        assert type(files) == list
       
        COMMASPACE = ','
       
        msg = MIMEMultipart('related')
        msg['From'] = fro
        msg['Subject'] = subject
        msg['To'] = COMMASPACE.join(to)
        msg['Cc'] = COMMASPACE.join(cc)
        msg['Date'] = formatdate(localtime=True)
        msg.set_charset('utf-8')
       
        msg.preamble = 'This is a multi-part message in MIME format.'
        msgAlternative = MIMEMultipart('alternative')    
        msg.attach(msgAlternative)  
       
        msgHtml = MIMEText(html, 'html', 'utf-8')    
        msgAlternative.attach(msgHtml)  
       
        for file in files:
            part = MIMEBase('application', 'octet-stream')
            part.set_payload(open(file, 'rb').read())
            encoders.encode_base64(part)
            part.add_header('Content-Disposition', 'attachment; filename=" % s"' % os.path.basename(file))
            msg.attach(part)
           
        self.__send(server, fro, to, msg)
   
    #发送普通的邮件
    def __send(self, server, fro, to, msg):
        assert type(server) == dict
        assert type(to) == list
       
        try:
            smtp = smtplib.SMTP(self.__smtpServer, 25)
            smtp.ehlo()
            smtp.starttls()
            smtp.ehlo()
            smtp.login(server.get('user'), server.get('passwd'))
            smtp.sendmail(fro, to, msg.as_string())
            smtp.close()
        except Exception , e:
            print traceback.format_exc()
